package run;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import com.sun.org.apache.bcel.internal.generic.Select;

import database.SetConn;

public class ClassBroswe {
	private Connection conn=null;
	private int num;
	private String[][]detail;
	public String[][] GetInfo(){
		return detail;
	}
	public int GetCount(){
		return num;
	}
	public void GetProduct(int ClassID){
		try{
            ResultSet rs;
			SetConn testconn=new SetConn();
			conn=testconn.setupConn();
			String count="select count(*) from commodity where ClassID=?";
			String sql="select * from commodity where ClassID=? ";
			PreparedStatement queryPs=conn.prepareStatement(count);
			queryPs.setInt(1, ClassID);
			rs=queryPs.executeQuery();
			if(rs.next()){
				num=rs.getInt(1);
			}
			else{
				return;
			}
			queryPs=conn.prepareStatement(sql);
			queryPs.setInt(1, ClassID);
			rs=queryPs.executeQuery();
			GetString(rs,num);
			return;
		}catch(Exception e){
			e.printStackTrace();
			return ;
		}
	}
	public void GetProduct(int class_one,int class_two){
		try{
            ResultSet rs;
			SetConn testconn=new SetConn();
			conn=testconn.setupConn();
			String count="select count(*) from commodity where ClassID between ? and ?";
			String sql="select * from commodity where ClassID between ? and ? ";
			PreparedStatement queryPs=conn.prepareStatement(count);
			queryPs.setInt(1, class_one);
			queryPs.setInt(2, class_two);
			rs=queryPs.executeQuery();
			if(rs.next()){
				num=rs.getInt(1);
			}
			else{
				return;
			}
			queryPs=conn.prepareStatement(sql);
			queryPs.setInt(1, class_one);
			queryPs.setInt(2, class_two);
			rs=queryPs.executeQuery();
			GetString(rs,num);
//			for(int i=0;i<num;i++){
//				System.out.println("line"+i+1);
//				System.out.println(detail[i][0]);
//				System.out.println(detail[i][1]);
//				System.out.println(detail[i][2]);
//				System.out.println(detail[i][3]);
//			}
			return ;
		}catch(Exception e){
			e.printStackTrace();
			return;
		}
	}
	private void GetString(ResultSet rs,int count){
		try{
			System.out.println(count);
		detail=new String[count][4];
		int i=0;
		while(rs.next()){
			detail[i][0]=rs.getString(4);
			detail[i][1]=rs.getString(5);
			detail[i][2]=Float.toString(rs.getFloat(6));
			detail[i][3]=Integer.toString(rs.getInt(8));
//			System.out.println(detail[i][0]);
//			System.out.println(detail[i][1]);
//			System.out.println(detail[i][2]);
//			System.out.println(detail[i][3]);
			i++;
		}
		return ;
		}catch(Exception e){
			e.printStackTrace();
			return ;
		}
	}
}
